package com.jh3502.vivo.function.login;

import android.support.annotation.NonNull;

import com.jh3502.vivo.base.BaseCallback;
import com.jh3502.vivo.base.BaseJson;
import com.jh3502.vivo.base.BasePresenter;
import com.jh3502.vivo.base.BaseView;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.utils.Utils;

import java.util.Map;

/**
 * @author harisucici
 * @date 2017/6/19
 */

public class LoginPresenter extends BasePresenter<LoginPresenter.LoginView> {
    LoginModel mLoginModel;

    LoginView mLoginView;

    public void setLoginView(LoginView mLoginView) {
        this.mLoginView = mLoginView;
    }

    public void toLogin(Map<String, Object> body) {
        mLoginView.showLoadingDialog();
        if (mLoginModel == null) {
            mLoginModel = new LoginModel(mLoginView.getActContext());
        }

        mLoginModel.login(body, new BaseCallback(mLoginView) {
            @Override
            public void responseSuccess(@NonNull BaseJson baseJson) {
                SpHelper.getInstance().saveAll(
                        Utils.getInstance().jso2map(baseJson.getObj().toString()));
                mLoginView.loginSuccess();
            }
        });
    }


    @Override
    public void cancelCall() {

    }

    public interface LoginView extends BaseView {
        void loginSuccess();
    }
}
